package estadopedidostiendasrn.com;

import java.sql.SQLException;

import estadopedidostiendasrn.com.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.CheckBox;
import android.widget.Toast;
import android.net.ConnectivityManager;

public class Login extends Activity {
	public static final String PREFS_NAME = "Applogs";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);        
        /* Check if we successfully logged in before. If we did, redirect to home page */
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		if (settings.getString("logged", "").toString().equals("logged")) {
			Intent intent = new Intent(Login.this, Home.class);
			startActivity(intent);
		}else{
	        Button b = (Button) findViewById(R.id.loginbutton);
	        TextView tvv = (TextView) findViewById(R.id.textMensaje);
	        //Disable button if wifi and mobile not connected
	        ConnectivityManager connMgr = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
	        final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
	        final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
	        if(!wifi.isConnected() && !mobile.isConnected()){
	        	b.setEnabled(false);
	        	Toast.makeText(this, "Redes no Disponibles", Toast.LENGTH_LONG).show();
	        	tvv.setText("Habilite la red para conectarse");
	        }
	        
			b.setOnClickListener(
				new OnClickListener() {
				public void onClick(View v) {
					MyDB db = new MyDB();
					EditText username = (EditText) findViewById(R.id.login);
					EditText password = (EditText) findViewById(R.id.password);
					TextView tv = (TextView) findViewById(R.id.textMensaje);
					
					if(username.getText().toString().length() > 0 && password.getText().toString().length() > 0 ) {
						
						if (db.dbConnect("jdbc:mysql://soportesiatec.dyndns.org/applogs","siatec","Siatec18046")){
							String resLogin=null;
							try {
								resLogin = db.loginApp(username.getText().toString(),password.getText().toString());
							} catch (SQLException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
								tv.setText("error de login");
							}
							if(username.getText().toString().equals(resLogin)) {
								/*
								 * So login information is correct, 
								 * we will save the Preference data
								 * and redirect to next class / home  
								 */
								SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
								SharedPreferences.Editor editor = settings.edit();
								editor.putString("logged", "logged");
								editor.commit();

								Intent intent = new Intent(Login.this, Home.class);
								startActivity(intent);							
							}else{
	                            tv.setText(resLogin + ":" + username.getText().toString());
							}
							db.closeConnection();
							db=null;
						}else{
							tv.setText("no conectado");
						}
					}
				}
			});
			
			CheckBox chk = (CheckBox) findViewById(R.id.chkShow);
			chk.setOnCheckedChangeListener(new OnCheckedChangeListener() {
	            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
	            	EditText password = (EditText) findViewById(R.id.password);
	            	if(isChecked) {
	                    password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
	                } else {
	                    password.setInputType(129);
	                }
	            }
	        });
		}
    }
}